package com.qcby.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qcby.dao.ExploreScriptDao;
import com.qcby.entity.ExploreScript;
import com.qcby.entity.PageResult;
import com.qcby.entity.Query.ExploreScriptQuery;
import com.qcby.entity.Shop;
import com.qcby.service.*;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class ExploreScriptServiceImpl implements ExploreScriptService {
    @Autowired
    private ExploreScriptDao exploreScriptDao;
    @Autowired
    private DeepSeekService deepSeekService;
    @Autowired
    private ShopService shopService;
    @Autowired
    private UserService userService;
    @Autowired
    private RecommendationService recommendationService;
    @Override
    public PageResult<ExploreScript> list(ExploreScriptQuery exploreScriptQuery) {
        PageHelper.startPage(exploreScriptQuery.getPage(), exploreScriptQuery.getPageSize());
        List<ExploreScript> page1 =  exploreScriptDao.list(exploreScriptQuery);
        System.out.println(page1);
        Page<ExploreScript> page2 = (Page<ExploreScript>) page1;
        PageResult<ExploreScript> pageResult = new PageResult<>(page2.getTotal(),page2.getResult());
        return pageResult;
    }

    @Override
    public void deleteById(Integer id) {
        exploreScriptDao.deleteById(id);
    }

    @Override
    public ExploreScript getById(Integer id) {
        ExploreScript exploreScript = exploreScriptDao.getById(id);
        return exploreScript;
    }

    @Override
    public List<Shop> getByIds(Integer[] ids) {
        List<Shop> exploreScripts = exploreScriptDao.getByIds(ids);
        return exploreScripts;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public List<ExploreScript> add(ExploreScriptQuery exploreScriptQuery) throws IOException {
        String count = exploreScriptQuery.getCount();
        String theme = exploreScriptQuery.getTheme();
        String title = exploreScriptQuery.getTitle();
        String desc = exploreScriptQuery.getDesc();
        String idsList = exploreScriptQuery.getIds();
        List<Integer> ids1 = new ArrayList<>();
        if(idsList!=null){
            String[] split = idsList.split(",");
            for(int i = 0;i<split.length;i++){
                ids1.add(Integer.parseInt(split[i]));
            }
        }

//        查找所有的用户
        List<Shop> shops = shopService.findAll1();
        String query = "请你从上述'"+shops+"'个商家中，按照主题为"+theme+"，标题为"+title+"，描述为"+desc+"，进行筛选，筛选出"+count+"店铺，并且把这些店铺的id值，只需要按照下面的形式返回[1,2,3]返回，如果没有合适的店铺，随便挑选三个返回[1,4,5]的形式 只返回[]这种形式,数组逗号之间没有空格";
        System.out.println(query);
        String json = deepSeekService.getJson(query);
        JSONObject jsonObject = JSONObject.parseObject(json);
        String res = jsonObject.getJSONArray("choices").getJSONObject(0).getJSONObject("message").getString("content");
        System.out.println(res);
        ExploreScript exploreScript = new ExploreScript();
        exploreScript.setTheme(theme);
        exploreScript.setTitle(title);
        exploreScript.setDescription(desc);
        exploreScript.setShopOrder(res);
        exploreScript.setCreatedAt(new Date());
        exploreScript.setLikeCount(0);
        exploreScript.setUsedCount(0);
        exploreScript.setViewCount(0);
        exploreScript.setFavoriteCount(0);
        exploreScript.setIsShared(0);
        exploreScript.setCoverImageUrl(exploreScriptQuery.getImg());
        Integer userId = userService.findByName(exploreScriptQuery.getUsername()).getId();
        exploreScript.setUserId(userId);
//        [1,2,3]
        String[] split = res.substring(1, res.length() - 1).split(",");
        List<Integer> ids = new ArrayList<>();
        for(int i = 0;i<split.length;i++){
            ids.add(Integer.parseInt(split[i]));
        }
        List<ExploreScript> list = exploreScriptDao.findListByIds(ids);
        System.out.println(list);
        System.out.println(ids);
//        保存用户的各种信息
        exploreScriptDao.add(exploreScript);
        return list;
    }
}
